package com.kxf.mvptest;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.kxf.mvptest.contract.GetPicContract;
import com.kxf.mvptest.contract.LoginContract;
import com.kxf.mvptest.listeners.LoginListener;
import com.kxf.mvptest.presenter.GetPicPresenterImpl;
import com.kxf.mvptest.presenter.LoginPresenterImpl;

public class MainActivity extends AppCompatActivity implements LoginContract.View, GetPicContract.View {

    private ProgressBar pb;
    private LoginContract.Presenter presenter = null;
    private ImageView iv;
    private GetPicContract.Presenter gpc = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pb = findViewById(R.id.pb);
        iv = findViewById(R.id.iv);
        presenter = new LoginPresenterImpl(this);
        gpc = new GetPicPresenterImpl(this);
    }

    public void loginIn(View view) {
        presenter.login(new LoginListener() {
            @Override
            public void onSuccess() {
                Log.d("MainActivity", "onSuccess");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pb.setVisibility(View.GONE);
                        Toast.makeText(MainActivity.this, getString(R.string.login_success), Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onFail() {
                Log.d("MainActivity", "onFail");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pb.setVisibility(View.GONE);
                        Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    @Override
    public void showLoading() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                pb.setVisibility(View.VISIBLE);
            }
        });
    }

    public void onGetPic(View view) {
        gpc.getPic(this);
    }

    @Override
    public void showPic(final Bitmap bmp) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                iv.setImageBitmap(bmp);
            }
        });
    }
}
